package com.zxy.supplier_system.server.entity

import jakarta.persistence.*
import org.hibernate.annotations.JdbcTypeCode
import org.hibernate.type.SqlTypes
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.OffsetDateTime


@EntityListeners(AuditingEntityListener::class)
@Entity
class EmployeeOperateRecord {

    companion object {
        fun withContent(content: List<ContentItem>): EmployeeOperateRecord {
            return EmployeeOperateRecord().apply {
                this.content = content
            }
        }

        fun withContent(vararg content: ContentItem): EmployeeOperateRecord {
            return withContent(content.toList())
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    @JdbcTypeCode(SqlTypes.JSON)
    var content: List<ContentItem> = listOf()

    @CreatedDate
    lateinit var createdDateTime: OffsetDateTime

    @ManyToOne(optional = false)
    @JoinColumn
    @CreatedBy
    lateinit var employee: Employee

    data class ContentItem(
        val text: String,
        val highlight: Boolean = false,
        val resource: ResourceTypeWithId? = null,
    ) {

        companion object{
            fun withResource(text: kotlin.String,type: ResourceType, id: String): ContentItem {
                return ContentItem(
                    text = text,
                    resource = ContentItem.ResourceTypeWithId(type, id)
                )
            }
        }

        enum class ResourceType {
            MarketOrder,
            MarketDeliveryOrder,
            CheckInventoryRecord,
            DirectSaleOrder,
            Procurement,
            PROCUREMENT_RECEIPT,
            PROCUREMENT_RETURN,
            MARKET_CHECK_INVENTORY_RECORD,
            MarketReturnOrder,
            MarketSaleDocument,
            MARKET,
            GOODS,
        }

        data class ResourceTypeWithId(
            val type: ResourceType,
            val id: String,
        )
    }


}